// 预付款充值
bangkeapp.controller('advance', 
	["$scope", "memberService", "$state", "$stateParams", "$ionicLoading", "$ionicModal", "accountService", "userinfoService",
    function($scope, memberService, $state, $stateParams, $ionicLoading, $ionicModal, accountService, userinfoService) {
    		
    		$ionicLoading.show({
			content: 'Loading',
			animation: 'fade-in',
			showBackdrop: true,
			maxWidth: 200,
			showDelay: 0
		});
    		
    		// 会员信息
    		$scope.member = userinfoService.getUser();
    		
        // 上传单据模块
        $ionicModal.fromTemplateUrl('advanceupload.html', {
            scope: $scope
        }).then(function(modal) {
            $scope.modal = modal;
        });
        
        // 提示成功模块
        $ionicModal.fromTemplateUrl('advanceuploadok.html', {
            scope: $scope
        }).then(function(modal) {
            $scope.okmodal = modal;
        });
        
        // 获取股权积分
        accountService.interests().then(function(result) {
        		$scope.account	= {};
            $scope.account.interests = result.interests;
        		$ionicLoading.hide();
        }, function(error) {
        }, function(progress) {
        });
    		
		$scope.toUpload = function(){
			$scope.modal.show();
		}
		$scope.toReUpload = function(){
			$scope.modal.show();
		}
		
		$scope.upload = function(){
			var sidebar = document.getElementsByClassName('file-hook');
			angular.element(sidebar)[0].click();
		};
		
		$scope.previewimag = function() {
			
			var base64 = new UploadBase64(event.target,  angular.element
				(document.getElementsByClassName('img-list'))[0]);
			
			angular.element(document.querySelector('.img-upload'))
				.removeClass("img-upload").addClass("img-uploaded");
		}
	
		$scope.deleteimg = function() {
		    angular.element(event.target).parent().remove();
		    angular.element(document.querySelector('.img-uploaded'))
				.removeClass("img-uploaded").addClass("img-upload");
		}
		$scope.submitimg = function() {
			$scope.uploadimg = angular.element(document.querySelector('.img-item img')).attr("src");

			$scope.imgshow = false;
	    		if (undefined == $scope.uploadimg) {
	    			$scope.imgshow = false;
	    		} else {
	    			$scope.imgshow = true;
	    		}
	    		
			$scope.modal.hide();
		}
		$scope.goback = function(){
			$scope.modal.hide();
		}
		
		$scope.advandeinapply = {};
		$scope.advandeinapply.taradvance = "6229 0846 6459 5225 19";
		
		$scope.submit = function() {
			$scope.advandeinapply.memberid = $scope.member.memberid;
			$scope.advandeinapply.amount = $scope.account.amount;
			$scope.advandeinapply.srcname = $scope.account.srcname;
			$scope.advandeinapply.img1 = $scope.uploadimg
			
			// 校验
			if (undefined == $scope.advandeinapply.memberid || "" == $scope.advandeinapply.memberid) {
				return false;
			}
			if (undefined == $scope.advandeinapply.amount || "" == $scope.advandeinapply.amount) {
				alert("您未输入金额!");
				return false;
			}
			if (undefined == $scope.advandeinapply.img1 || "" == $scope.advandeinapply.img1) {
				alert("您未上传单据图片!");
				return false;
			}
			if (undefined == $scope.advandeinapply.taradvance || "" == $scope.advandeinapply.taradvance) {
				return false;
			}
			if (undefined == $scope.advandeinapply.srcname || "" == $scope.advandeinapply.srcname) {
				alert("您未输入转账人姓名!");
				return false;
			}
			
			memberService.advandeinapplyupload($scope.advandeinapply).then(function(result) {
				$ionicLoading.hide();
				$scope.okmodal.show();
				if ("M" == result.Response.Header.ResponseCode){
			        $scope.status = "成功";
			        $scope.statusdesc = "系统已收到您的申请, 待管理员审核通过后, 为您完成预付款充值, 敬请耐心等待!";
			        $scope.btndesc = "完成";
				} else {
					$scope.status = "失败";
					$scope.statusdesc = result.Response.Header.MsgList[0].MsgContent;
					$scope.btndesc = "返回";
				}
			},
			function(error) {
				$scope.show = false;
				$scope.data = "error!";
			},
			function(progress) {
				$scope.show = true;
			});
		}
		
		$scope.tomemberhome = function(){
			$scope.okmodal.hide();
			$state.go("bangke.memberhome");
		}
    }
]);